<?php

class BrandsController extends AppController {

    var $name = 'Brands';
    var $uses = array('Categorybrand', 'Brand', 'Category', 'Product');

    function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allowedActions = array('*');
    }

    function index($id=null) {
        $this->layout = "carthome";
        //Fetch category 1st level
        $this->Category->recursive = 0;
        $categories = $this->Category->find('all', array(
            'conditions' => array('Category.parent_id' => '0'),
            'order' => 'Category.category_order ASC',
            'fields' => 'Category.category_name'
                ));
        //Fetch child category
        if (!empty($id))
            $parent_id = $id;
        else
            $parent_id = $categories['0']['Category']['id'];
        /*
          $child_categories=$this->Category->viewAll($parent_id);
          $cats[$parent_id]=$parent_id;
          foreach($child_categories as $child_category){
          $cats[$child_category['id']]=$child_category['id'];
          }
         */
        //Fetch Brands list
        //$fields=array('Brand.id','Brand.brand_description');
        //$conditions=array('Categorybrand.category_id'=>$cats,'Categorybrand.brand_type'=>'brand');
        //$order=array('Brand.brand_description' => 'asc');
        $this->set(compact('categories', 'parent_id'));
    }

    function fetchbrands($id1=null, $id2=null) {
        $this->layout = false;
        //$this->autoRender=false;
        //Fetch child category
        $brands = $this->Categorybrand->brandindex($id1, $id2);
        $this->Product->Behaviors->attach('Containable');
        $brands = $this->Product->find('all', array('fields' =>
            array('Product.id', 'count(Product.global_brands) as totalproduct'),
            'conditions' => array('Product.global_brands' => Set::extract('/Brand/id', $brands), 'Product.products_expiredDate >=' => date('Y-m-d H:i:s'), 'OR' => array(array('Product.products_status !=' => 'Disapproved'), array('Product.products_status !=' => 'Pending'))),
            'contain' => array('Brand.id', 'Brand.brand_description'),
            'group' => 'Product.global_brands'));
        $this->set('brands', $brands);
    }

}

?>